import React, {Component} from 'react'
import PropTypes from 'prop-types'
import './index.css'

export default class Header extends Component {
  static propTypes = {
    todos: PropTypes.array.isRequired,
    addToDo: PropTypes.func.isRequired
  }

  render() {
    return (
      <div className="todo-header">
        <input onKeyUp={this.handleAddTodo} type="text" placeholder="请输入你的任务名称，按回车键确认" />
      </div>
    )
  }

  handleAddTodo = (e) => {
    // 判断按键 e.key === 'Enter'
    if (e.key === 'Enter' && e.target.value.trim() !== '') {
      const name = e.target.value
      const addToDo = this.props.addToDo
      addToDo(name)
      e.target.value = ''
    }
  }
}
